package com.gxa.controller.asset;

import com.github.pagehelper.PageInfo;
import com.gxa.common.utils.JWTUtils;
import com.gxa.common.utils.R;
import com.gxa.entity.asset.Equipment;
import com.gxa.entity.asset.search.EquipmentSearchMap;
import com.gxa.service.asset.EquipmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import static com.gxa.common.utils.R.error;
import static com.gxa.common.utils.R.ok;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yjl
 * @since 2022-10-12 11:37:51
 */
@Api(tags = "设备管理--->设备台账")
@RestController
@RequestMapping("/equipment")
public class EquipmentController {

    @Autowired
    private EquipmentService equipmentService;

//    @RequiresPermissions("emp:list1")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("设备管理列表接口")
    @GetMapping("/list")
    public R queryAll(EquipmentSearchMap map ) {
        PageInfo<Equipment> pageInfo = equipmentService.queryByPage(map);

        return ok("successs").put("data", pageInfo).put("locations",equipmentService.installLocations());
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("新增设备管理接口")
    @PostMapping("/add")
    public R add(@RequestBody Equipment device) {
        boolean result = this.equipmentService.save(device);
        if (result) {
            return ok("添加成功");
        }
        return error();
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("删除设备管理接口")
    @DeleteMapping("/del/{id}")
    public R del(@PathVariable("id") Integer id) {
        boolean result = equipmentService.removeById(id);
        if (result) {
            return ok("删除成功");
        }
        return ok("del error");
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("根据id查询设备管理接口")
    @GetMapping("/queryById/{id}")
    public R queryById(@PathVariable("id") Integer id) {
        Equipment equipment = equipmentService.getById(id);
        if (equipment != null) {
            return ok().put("data", equipment);
        }
        return error("query error");
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("编辑设备管理接口")
    @PutMapping("/edit")
    public R edit(@RequestBody Equipment device) {

        boolean result = equipmentService.updateById(device);
        if (result) {
            return ok("编辑成功");
        }
        return ok("edit error");
    }

}
